/**
 * 初始化商品信息详情对话框
 */
var GoodsInfoDlg = {
    goodsInfoData : {},
    classify :{},
    load :true,
    editload :true,
    validateFields: {
        goodName: {
            validators: {
                notEmpty: {
                    message: '商品名称不能为空'
                }
            }
        },
        goodIcon: {
            validators: {
                notEmpty: {
                    message: '商品图片不能为空'
                }
            }
        },
        classtype: {
            validators: {
                notEmpty: {
                    message: '分类不能为空'
                }
            }
        },
        goodInventory: {
            validators: {
                notEmpty: {
                    message: '存库不能为空'
                },
                integer: {
                    message: '输入非法数字'
                }
            }
        },
        goodPrice: {
            validators: {
                notEmpty: {
                    message: '价格不能为空'
                },
                integer: {
                    message: '输入非法数字'
                }
            }
        },
        productStatus: {
            validators: {
                notEmpty: {
                    message: '状态不能为空'
                }
            }
        }
    }

};



/**
 * 清除数据
 */
GoodsInfoDlg.clearData = function() {
    this.goodsInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
GoodsInfoDlg.set = function(key, val) {
    this.goodsInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
GoodsInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
GoodsInfoDlg.close = function() {
    parent.layer.close(window.parent.Goods.layerIndex);
}

/**
 * 收集数据
 */
GoodsInfoDlg.collectData = function() {
    this
    .set('goodId')
    .set('categoryType')
    .set('goodDescription')
    .set('goodIcon')
    .set('goodInventory')
    .set('goodName')
    .set('goodPrice')
    .set('productStatus');
}

/**
 * 提交添加
 */
GoodsInfoDlg.addSubmit = function() {

    this.clearData();
    var options=$("#classtype option:selected");
    for(var i=0;i<GoodsInfoDlg.classify.length;i++){
        if(GoodsInfoDlg.classify[i].fullname==options.val()){
            $("#categoryType").val(GoodsInfoDlg.classify[i].id);
        }
    }
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/goods/add", function(data){
        Feng.success("添加成功!");

    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.goodsInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
GoodsInfoDlg.editSubmit = function() {

    this.clearData();
    var options=$("#goodtype").find("option:selected").text();
    for(var i=0;i<GoodsInfoDlg.classify.length;i++){
        if(GoodsInfoDlg.classify[i].fullname==options){
            $("#categoryType").val(GoodsInfoDlg.classify[i].id);
        }
    }
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/goods/update", function(data){
        Feng.success("修改成功!");
        window.parent.Goods.table.refresh();
        GoodsInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.goodsInfoData);
    console.log(this.goodsInfoData);
    ajax.start();
}
/**
 * 获取商品分类信息
 */
GoodsInfoDlg.getClassify=function () {
    if(GoodsInfoDlg.load){
        for (var i = 0; i < GoodsInfoDlg.classify.length; i++) {
            $("#classtype").append("<option>" + GoodsInfoDlg.classify[i].fullname + "</option>");
        }
        GoodsInfoDlg.load=false;
    }
}
/**
 * 验证数据是否为空
 */
GoodsInfoDlg.validate = function () {
    $('#goodInfoForm').data("bootstrapValidator").resetForm();
    $('#goodInfoForm').bootstrapValidator('validate');
    return $("#goodInfoForm").data('bootstrapValidator').isValid();
};

$(function() {

    Feng.initValidator("goodInfoForm", GoodsInfoDlg.validateFields);
    //设置商品状态
    $("#productStatus").val($("#status").val());

    var avatarUp = new $GoodUpload("goodIcon",Feng.ctxPath + '/goods/upload');
    avatarUp.setUploadBarId("progressBar");
    avatarUp.init();


    var ajax = new $ax(Feng.ctxPath + "/goodsClassify/getlist", function(data){
        GoodsInfoDlg.classify=data;
    },function(data){});
    ajax.start();


    if(GoodsInfoDlg.editload){
        for (var i = 0; i < GoodsInfoDlg.classify.length; i++) {
            $("#goodtype").append("<option value='GoodsInfoDlg.classify[i].id'>" + GoodsInfoDlg.classify[i].fullname + "</option>");
        }
        for (var i = 0; i < GoodsInfoDlg.classify.length; i++) {
            if(GoodsInfoDlg.classify[i].id==$("#categoryType").val()){
                $("#goodtype").get(0).options[i].selected = true;
            }
        }
        GoodsInfoDlg.editload=false;

    }

});
